
clear


ref2=[
3.35, 4.82e-3
3.35, 4.99e-3
3.35, 5.17e-3
3.35, 5.34e-3
3.35, 5.51e-3
3.6, 5.69e-3
3.6, 5.87e-3
3.6, 6.04e-3
3.6, 6.21e-3
3.6, 6.39e-3
3.6, 6.56e-3
3.6, 6.73e-3
3.6, 6.91e-3
3.6, 7.08e-3
3.6, 7.26e-3
3.87, 7.43e-3
3.87, 7.61e-3
3.87, 7.78e-3
3.87, 7.96e-3
3.87, 8.13e-3
3.88, 8.3e-3
3.88, 8.48e-3
3.88, 8.65e-3
3.88, 8.82e-3
3.88, 9.0e-3
4.14, 9.18e-3
4.14, 9.35e-3
4.14, 9.52e-3
4.14, 9.7e-3
4.14, 9.87e-3
4.14, 1.0e-2
4.14, 1.02e-2
4.14, 1.04e-2
4.14, 1.06e-2
4.14, 1.07e-2
4.14, 1.09e-2
4.4, 1.11e-2
4.4, 1.13e-2
4.4, 1.14e-2
4.4, 1.16e-2
4.4, 1.18e-2
4.4, 1.2e-2
4.4, 1.21e-2
4.66, 1.23e-2
4.66, 1.25e-2
4.66, 1.27e-2
4.66, 1.28e-2
4.66, 1.3e-2
4.66, 1.32e-2
4.66, 1.34e-2
4.92, 1.35e-2
4.92, 1.37e-2
4.92, 1.39e-2
4.92, 1.41e-2
4.92, 1.42e-2
4.93, 1.44e-2
4.93, 1.46e-2
5.18, 1.48e-2
5.18, 1.49e-2
5.18, 1.51e-2
5.19, 1.53e-2
5.19, 1.54e-2
5.19, 1.56e-2
5.44, 1.58e-2
5.44, 1.6e-2
5.44, 1.61e-2
5.45, 1.63e-2
5.45, 1.65e-2
5.45, 1.67e-2
5.45, 1.68e-2
5.45, 1.7e-2
5.45, 1.72e-2
5.71, 1.74e-2
5.71, 1.75e-2
5.71, 1.77e-2
5.71, 1.79e-2
5.71, 1.81e-2
5.71, 1.82e-2
5.71, 1.84e-2
5.71, 1.86e-2
5.71, 1.88e-2
5.71, 1.89e-2
5.97, 1.91e-2
5.97, 1.93e-2
5.97, 1.95e-2
5.97, 1.96e-2
5.97, 1.98e-2
5.97, 2.0e-2
5.97, 2.02e-2
5.97, 2.03e-2
5.98, 2.05e-2
5.98, 2.07e-2
5.98, 2.08e-2
6.23, 2.1e-2
6.23, 2.12e-2
6.24, 2.14e-2
6.24, 2.15e-2
6.24, 2.17e-2
6.24, 2.19e-2
6.24, 2.21e-2
6.24, 2.22e-2
6.24, 2.24e-2
6.24, 2.26e-2
6.5, 2.28e-2
6.5, 2.29e-2
6.5, 2.31e-2
6.5, 2.33e-2
6.5, 2.35e-2
6.5, 2.36e-2
6.5, 2.38e-2
6.5, 2.4e-2
6.5, 2.42e-2
6.5, 2.43e-2
6.76, 2.45e-2
6.76, 2.47e-2
6.76, 2.49e-2
6.76, 2.5e-2
6.76, 2.52e-2
6.76, 2.54e-2
6.77, 2.56e-2
6.77, 2.57e-2
7.02, 2.59e-2
7.02, 2.61e-2
7.02, 2.63e-2
7.02, 2.64e-2
7.03, 2.66e-2
7.03, 2.68e-2
7.03, 2.69e-2
7.03, 2.71e-2
7.28, 2.73e-2
7.29, 2.75e-2
7.29, 2.76e-2
7.29, 2.78e-2
7.29, 2.8e-2
7.29, 2.82e-2
7.29, 2.83e-2
7.29, 2.85e-2
7.55, 2.87e-2
7.55, 2.89e-2
7.55, 2.9e-2
7.55, 2.92e-2
7.55, 2.94e-2
7.81, 2.94e-2
8.06, 2.96e-2
8.06, 2.97e-2
8.32, 2.99e-2
8.58, 3.01e-2
8.58, 3.03e-2
8.58, 3.04e-2
8.58, 3.06e-2
8.58, 3.08e-2
8.58, 3.1e-2
8.83, 3.11e-2
8.83, 3.13e-2
8.83, 3.15e-2
8.83, 3.17e-2
8.83, 3.18e-2
8.83, 3.2e-2
8.83, 3.22e-2
8.83, 3.24e-2
9.1, 3.25e-2
9.1, 3.27e-2
9.1, 3.29e-2
9.1, 3.31e-2
9.35, 3.32e-2
9.6, 3.32e-2
9.6, 3.34e-2
9.87, 3.34e-2
10.13, 3.36e-2
10.13, 3.38e-2
10.13, 3.39e-2
10.39, 3.41e-2
10.39, 3.43e-2
10.64, 3.45e-2
10.64, 3.46e-2
10.9, 3.48e-2
10.9, 3.5e-2
11.16, 3.52e-2
11.16, 3.53e-2
11.42, 3.55e-2
11.42, 3.57e-2
11.42, 3.59e-2
11.42, 3.6e-2
11.42, 3.62e-2
11.68, 3.64e-2
11.68, 3.66e-2
11.68, 3.67e-2
11.68, 3.69e-2
11.68, 3.71e-2
11.68, 3.73e-2
11.68, 3.74e-2
11.94, 3.76e-2
11.94, 3.78e-2
11.94, 3.8e-2
11.94, 3.81e-2
11.94, 3.83e-2
11.94, 3.85e-2
11.94, 3.87e-2
12.2, 3.88e-2
12.2, 3.9e-2
12.2, 3.92e-2
12.2, 3.93e-2
12.2, 3.95e-2
12.2, 3.97e-2
12.46, 3.99e-2
12.46, 4.0e-2
12.46, 4.02e-2
12.46, 4.04e-2
12.46, 4.02e-2
12.71, 4.01e-2
12.97, 4.02e-2
13.23, 4.04e-2
13.49, 4.06e-2
13.74, 4.06e-2
13.74, 4.08e-2
14.0, 4.09e-2
14.25, 4.09e-2
14.51, 4.11e-2
14.51, 4.13e-2
14.77, 4.15e-2
15.02, 4.15e-2
15.03, 4.16e-2
15.28, 4.18e-2
15.54, 4.18e-2
15.8, 4.2e-2
15.8, 4.22e-2
16.05, 4.22e-2
16.31, 4.24e-2
16.56, 4.25e-2
16.81, 4.27e-2
17.08, 4.29e-2
17.33, 4.27e-2
17.33, 4.25e-2
17.58, 4.24e-2
17.83, 4.22e-2
17.83, 4.2e-2
18.1, 4.18e-2
18.1, 4.17e-2
18.35, 4.15e-2
18.6, 4.13e-2
18.85, 4.12e-2
18.85, 4.1e-2
19.12, 4.08e-2
19.12, 4.06e-2
19.37, 4.06e-2
19.37, 4.05e-2
19.63, 4.03e-2
19.63, 4.01e-2
19.88, 4.0e-2
20.14, 3.98e-2
20.39, 3.96e-2
20.39, 3.94e-2
20.65, 3.93e-2
20.65, 3.91e-2
20.9, 3.89e-2
21.16, 3.88e-2
21.16, 3.86e-2
21.41, 3.84e-2
21.67, 3.82e-2
21.67, 3.81e-2
21.92, 3.81e-2
21.92, 3.79e-2
22.18, 3.77e-2
22.18, 3.75e-2
22.43, 3.74e-2
22.43, 3.72e-2
22.43, 3.7e-2
22.43, 3.69e-2
22.68, 3.67e-2
22.68, 3.65e-2
22.68, 3.63e-2
22.68, 3.62e-2
22.68, 3.6e-2
22.68, 3.58e-2
22.68, 3.56e-2
22.94, 3.55e-2
22.93, 3.53e-2
22.93, 3.51e-2
22.93, 3.49e-2
22.93, 3.48e-2
22.93, 3.46e-2
22.93, 3.44e-2
23.19, 3.43e-2
23.19, 3.41e-2
23.18, 3.39e-2
23.18, 3.37e-2
23.18, 3.36e-2
23.18, 3.34e-2
23.18, 3.32e-2
23.18, 3.3e-2
23.44, 3.29e-2
23.44, 3.27e-2
23.43, 3.25e-2
23.43, 3.23e-2
23.69, 3.22e-2
23.69, 3.2e-2
23.69, 3.18e-2
23.69, 3.16e-2
23.94, 3.15e-2
23.94, 3.13e-2
23.94, 3.11e-2
23.94, 3.1e-2
24.2, 3.08e-2
24.19, 3.06e-2
24.19, 3.04e-2
24.45, 3.03e-2
24.45, 3.01e-2
24.45, 2.99e-2
24.45, 2.97e-2
24.7, 2.96e-2
24.7, 2.94e-2
24.7, 2.92e-2
24.7, 2.91e-2
24.96, 2.89e-2
24.95, 2.87e-2
24.95, 2.85e-2
24.95, 2.84e-2
25.21, 2.82e-2
25.21, 2.8e-2
25.46, 2.78e-2
25.46, 2.77e-2
25.46, 2.75e-2
25.46, 2.73e-2
25.72, 2.71e-2
25.72, 2.7e-2
25.71, 2.68e-2
25.97, 2.66e-2
25.97, 2.65e-2
26.22, 2.63e-2
26.22, 2.61e-2
26.22, 2.59e-2
26.22, 2.58e-2
26.48, 2.56e-2
26.48, 2.54e-2
26.48, 2.52e-2
26.73, 2.51e-2
26.73, 2.49e-2
26.73, 2.47e-2
26.98, 2.46e-2
26.98, 2.44e-2
26.98, 2.42e-2
26.98, 2.4e-2
27.24, 2.39e-2
27.24, 2.37e-2
27.24, 2.35e-2
27.49, 2.33e-2
27.49, 2.32e-2
27.49, 2.3e-2
27.75, 2.28e-2
27.74, 2.26e-2
27.74, 2.25e-2
28.0, 2.23e-2
28.0, 2.21e-2
28.25, 2.2e-2
28.51, 2.18e-2
28.76, 2.16e-2
29.02, 2.14e-2
29.02, 2.13e-2
29.27, 2.11e-2
29.27, 2.09e-2
29.53, 2.08e-2
29.78, 2.06e-2
29.78, 2.04e-2
29.78, 2.02e-2
30.04, 2.01e-2
30.04, 1.99e-2
30.29, 1.97e-2
30.29, 1.95e-2
30.55, 1.94e-2
30.55, 1.92e-2
30.8, 1.92e-2
30.8, 1.9e-2
30.8, 1.89e-2
31.06, 1.87e-2
31.05, 1.85e-2
31.31, 1.83e-2
31.31, 1.82e-2
31.56, 1.8e-2
31.56, 1.78e-2
31.56, 1.76e-2
31.81, 1.77e-2
31.81, 1.75e-2
32.07, 1.73e-2
32.07, 1.71e-2
32.33, 1.7e-2
32.33, 1.68e-2
32.58, 1.66e-2
32.58, 1.64e-2
32.58, 1.63e-2
32.83, 1.61e-2
33.09, 1.59e-2
33.09, 1.58e-2
33.35, 1.56e-2
33.35, 1.54e-2
33.34, 1.52e-2
33.6, 1.51e-2
33.6, 1.49e-2
33.85, 1.47e-2
34.11, 1.45e-2
34.11, 1.44e-2
34.37, 1.44e-2
34.35, 1.42e-2
34.62, 1.4e-2
34.62, 1.39e-2
34.87, 1.37e-2
35.13, 1.35e-2
35.38, 1.33e-2
35.38, 1.32e-2
35.64, 1.3e-2
35.89, 1.28e-2
36.15, 1.27e-2
36.15, 1.25e-2
36.4, 1.23e-2
36.4, 1.21e-2
36.66, 1.21e-2
36.92, 1.2e-2
36.91, 1.18e-2
37.17, 1.16e-2
37.17, 1.14e-2
37.42, 1.13e-2
37.68, 1.11e-2
37.93, 1.09e-2
37.93, 1.08e-2
38.19, 1.06e-2
38.44, 1.04e-2
38.7, 1.02e-2
38.7, 1.01e-2
38.95, 9.9e-3
38.95, 9.73e-3
39.21, 9.73e-3
39.21, 9.56e-3
39.46, 9.39e-3
39.46, 9.21e-3
39.72, 9.04e-3
39.97, 8.87e-3
40.23, 8.7e-3
40.48, 8.53e-3
40.48, 8.35e-3
40.74, 8.18e-3
40.99, 8.01e-3
41.25, 7.84e-3
41.51, 7.84e-3
41.76, 7.85e-3
41.76, 7.67e-3
42.02, 7.67e-3
42.02, 7.5e-3
42.27, 7.5e-3
42.53, 7.33e-3
42.78, 7.33e-3
42.78, 7.16e-3
43.04, 7.16e-3
43.29, 6.99e-3
43.55, 7.0e-3
43.81, 6.82e-3
44.06, 6.83e-3
44.06, 6.65e-3
44.32, 6.66e-3
44.56, 6.48e-3
44.83, 6.49e-3
44.83, 6.31e-3
45.08, 6.32e-3
45.34, 6.32e-3
45.08, 6.14e-3
44.83, 6.14e-3
44.56, 6.31e-3
44.56, 6.14e-3
44.56, 5.96e-3
44.81, 5.79e-3
45.08, 5.62e-3
45.34, 5.45e-3
45.58, 5.28e-3
45.58, 5.1e-3
45.85, 4.93e-3
45.83, 4.76e-3
46.1, 4.59e-3
46.35, 4.42e-3
46.35, 4.24e-3
46.35, 4.07e-3
46.6, 4.07e-3
46.87, 4.07e-3
47.12, 4.08e-3
47.38, 3.91e-3
47.63, 3.91e-3
47.89, 3.91e-3
48.14, 3.91e-3
48.4, 3.74e-3
48.66, 3.75e-3
48.91, 3.75e-3
49.17, 3.75e-3
49.42, 3.75e-3
49.68, 3.58e-3
49.94, 3.59e-3
50.19, 3.59e-3
50.45, 3.59e-3
50.7, 3.59e-3
50.96, 3.6e-3
51.21, 3.43e-3
51.47, 3.43e-3
51.47, 3.26e-3
51.73, 3.08e-3
51.98, 3.09e-3
52.24, 2.92e-3
52.49, 2.92e-3
52.75, 2.75e-3
53.0, 2.75e-3
53.26, 2.75e-3
53.52, 2.58e-3
53.77, 2.58e-3
54.03, 2.59e-3
54.28, 2.42e-3
54.54, 2.42e-3
54.79, 2.42e-3
55.05, 2.42e-3
55.31, 2.43e-3
55.56, 2.43e-3
55.81, 2.43e-3
56.07, 2.44e-3
56.33, 2.44e-3
56.58, 2.44e-3
56.83, 2.44e-3
57.1, 2.45e-3
57.35, 2.28e-3
57.35, 2.1e-3
57.6, 2.1e-3
57.6, 1.93e-3
57.6, 1.76e-3
57.85, 1.59e-3
58.12, 1.59e-3
58.37, 1.59e-3
58.63, 1.59e-3
58.63, 1.42e-3
58.89, 1.42e-3
59.14, 1.43e-3
59.4, 1.43e-3
59.65, 1.6e-3
59.91, 1.61e-3
60.17, 1.61e-3
60.42, 1.79e-3
60.68, 1.79e-3
60.94, 1.79e-3
61.19, 1.8e-3
61.45, 1.8e-3
61.7, 1.8e-3
61.96, 1.8e-3
62.21, 1.63e-3
62.47, 1.64e-3
62.73, 1.64e-3
62.98, 1.64e-3
63.24, 1.64e-3
63.5, 1.65e-3
63.75, 1.65e-3
64.01, 1.65e-3
64.26, 1.66e-3
64.52, 1.66e-3
64.77, 1.49e-3
65.03, 1.49e-3
65.29, 1.49e-3
65.54, 1.32e-3
65.8, 1.32e-3
66.05, 1.33e-3
66.31, 1.33e-3
66.56, 1.33e-3
66.81, 1.34e-3
67.08, 1.34e-3
67.33, 1.34e-3
67.58, 1.34e-3
67.85, 1.35e-3
68.1, 1.35e-3
68.35, 1.35e-3
68.6, 1.35e-3
68.87, 1.36e-3
69.13, 1.36e-3
69.38, 1.36e-3
69.64, 1.37e-3
69.89, 1.37e-3
70.15, 1.37e-3
70.41, 1.37e-3
70.66, 1.38e-3
70.92, 1.55e-3
71.17, 1.56e-3
71.43, 1.56e-3
71.69, 1.56e-3
71.94, 1.56e-3
72.2, 1.57e-3
72.45, 1.57e-3
72.71, 1.57e-3
72.97, 1.58e-3
73.22, 1.58e-3
73.48, 1.58e-3
73.73, 1.41e-3
73.99, 1.41e-3
74.25, 1.42e-3
74.5, 1.42e-3
74.76, 1.42e-3
75.01, 1.42e-3
75.27, 1.43e-3
75.53, 1.43e-3
75.78, 1.43e-3
76.04, 1.44e-3
76.29, 1.44e-3
76.55, 1.44e-3
76.81, 1.44e-3
77.06, 1.45e-3
77.32, 1.45e-3
77.56, 1.45e-3
77.83, 1.45e-3
78.09, 1.46e-3
78.34, 1.46e-3
78.6, 1.46e-3
78.85, 1.47e-3
79.11, 1.47e-3
79.37, 1.47e-3
];

%[kx,ky]=

ref2(:,2)=ref2(:,2)-min(ref2(:,2));



%=============give pore size distribution here=======================

ref1=ref2;
pore_r_min=1;
pore_r_max=80;
%======================================================================




ref1(:,2)=ref1(:,2)-min(ref1(:,2));










r_min=1;
r_max=80;
r_ave=5;
sigma=16.0;
kappa=1.0;
%===========parameter for micromodels=============
d=10;
b=d/5*8;
a=d/5*24;
c=d/5*50;
left=round(c*1);    %=========parameter==========
right=round(c*1);
up=round(c*0.8);
down=round(c*1);
reservior=120;
lx=10;               %junction number in X direction
ly=5;               %junction number in Y direction
%====================================================

r=r_min:0.05:r_max;



%======================USE EQUATIONS=======================================
% P=kappa*(r_max-r).*(r-r_min).*exp(-(r-r_ave).^2/(2*sigma^2));
% %P=kappa*(r_max-r).*(r-r_min)
% %plot (r,P);
% 
% 
% 
% 
% rrr=r;
% PPP=P;
% ss=trapz(r,P);
% 
% num=[];
% r=r_min:0.05:r_min+0.5;
% P=kappa*(r_max-r).*(r-r_min).*exp(-(r-r_ave).^2/(2*sigma^2));
% num=[num,trapz(r,P)];
% 
% for i=r_min+1:r_max-1
%     r=i-0.5:0.05:i+0.5;
%     P=kappa*(r_max-r).*(r-r_min).*exp(-(r-r_ave).^2/(2*sigma^2));
%     num=[num,trapz(r,P)];
% end
% 
% r=r_max-0.5:0.05:r_max;
% P=kappa*(r_max-r).*(r-r_min).*exp(-(r-r_ave).^2/(2*sigma^2));
% num=[num,trapz(r,P)];
% 
% num=num/ss;
% plot(num);
% hold on
% plot (ref2(:,1),ref2(:,2),'x');
% hold off
% 
% 
% dist=[0.0];tmp=0;
% for i=1:r_max-r_min+1
%     tmp=tmp+num(i);
%     dist=[dist,tmp];
% end
%==============================================================




%=======for channels=========================================


mimax=size(ref2,1);%ss=trapz(ref1(:,1),ref1(:,2));
dist=[0.0];tmp=0;mi=1;num=[];tmp2=0;
for i=1:r_max-r_min+1
    tmp2=0;
    if (mi<mimax)
    while (mi<mimax) && (ref2(mi,1)<i-r_min+1)
        tmp=tmp+ref2(mi,2)*(ref2(mi+1,1)-ref2(mi,1));
        tmp2=tmp2+ref2(mi,2)*(ref2(mi+1,1)-ref2(mi,1));
        mi=mi+1;
    end
    
    dist=[dist,tmp];
    end
    num=[num,tmp2];
end
dist=dist/tmp;
%num=num/tmp;
%============================================================



%=======for pores=========================================



mimax=size(ref1,1);%ss=trapz(ref1(:,1),ref1(:,2));
dist2=[0.0];tmp=0;mi=1;
for i=1:pore_r_max-pore_r_min+1
    if (mi<mimax)
      
        
    while (mi<mimax) && (ref1(mi,1)<i-pore_r_min+1)
        tmp=tmp+ref1(mi,2)*(ref1(mi+1,1)-ref1(mi,1));
        mi=mi+1;
    end
    
    dist2=[dist2,tmp];
    end
    
end
dist2=dist2/tmp;
%============================================================



%=========================VALIDATION=========================
% test_data=[];
% for i=1:1000
%     tmp=rand;mark=0;
%     for (j=2:r_max-r_min+2)
%         if (tmp>=dist(j-1)) && (tmp<dist(j)) &&(mark==0)
%             test_data=[test_data,j-1];
%             mark=1;
%         end
%     end
% end
% 
% sta=zeros(r_max-r_min+1);
% for i=1:1000
%     sta(test_data(i))=sta(test_data(i))+1;
% end
% figure
% 
% plot(sta)
%============================================================




%======Random==channels=====
ch_int=r_min:r_max;
ch_int=ceil(ch_int/2);

% ch1=d/5*3;
% ch2=d/5*5;
% ch3=d/5*6.4;
% ch4=d/5*10.4;

%ch_int=[round(ch1/2),round(ch2/2),round(ch3/2),round(ch4/2)];
%ch_int=[floor(ch1/2),floor(ch2/2),floor(ch3/2),floor(ch4/2)];
% ch_int=[ceil(ch1/2),ceil(ch2/2),ceil(ch3/2),ceil(ch4/2)];
%===========================
% a2=round(a/2);
% b2=round(b/2);
% c2=round(c/2);


a2=floor(a/2);
b2=floor(b/2);
c2=floor(c/2);
%===============================




xdir=[left];
ydir=[down];

for i=1:lx-1
  xdir=[xdir,c];
end
for i=1:ly-1
  ydir=[ydir,c];
end
xdir=[xdir,right];
ydir=[ydir,up];

%ydir=[down,c,c,c,up];
%xdir=[left,c,c,c,right];
nx=left+c*(lx-1)+right;
ny=up+down+reservior+c*(ly-1);

pa=ones(nx,ny);


for i=1:nx
    for j=1:reservior
        pa(i,j)=0;
    end
end

%===========make pores==================
% for indx=1:lx
%     for indy=1:ly
%         corx=(indx-1)*c+left;
%         cory=reservior+(indy-1)*c+down;
%         for (i=corx-a2:corx+a2)
%             for (j=cory-a2:cory+a2)
%                 pa(i,j)=0;
%             end
%         end
%         
%     
%     end
% end
%=========================================



%===========make random pores==================
deb2=[];texts2=[];
for indx=1:lx
    for indy=1:ly
        corx=(indx-1)*c+left;
        cory=reservior+(indy-1)*c+down; 
        
        tmp=rand;mark=0;
    for (j=2:pore_r_max-pore_r_min+2)
        if (tmp>=dist2(j-1)) && (tmp<dist2(j)) &&(mark==0)
            test_data=j-1;
            mark=1;
        end
    end
        chw=test_data;deb2=[deb2,chw];
         %-------------------------
         
         %------------------------
         texts2=[texts2;[corx,cory,chw]];
         a2=chw;
         %------------------------
        for (i=corx-a2:corx+a2)
            for (j=cory-a2:cory+a2)
                pa(i,j)=0;
            end
        end
        
    
    end
end
%=========================================



deb=[];texts=[];shift=5;
%===============Random===channels==========

cory2=reservior;
cory1=reservior;
for indy=1:ly+1
    cory2=cory2+ydir(indy);
    
    for indx=1:lx
    
        corx=(indx-1)*c+left;
    
        %-------------------------
      tmp=rand;mark=0;
    for (j=2:r_max-r_min+2)
        if (tmp>=dist(j-1)) && (tmp<dist(j)) &&(mark==0)
            test_data=j-1;
            mark=1;
        end
    end
        chw=test_data;deb=[deb,chw];
         %-------------------------
         
         %------------------------
         texts=[texts;[corx-ch_int(chw)-shift,(cory1+cory2)/2,chw]];
         %------------------------
        for i=corx-ch_int(chw):corx+ch_int(chw)
            for j=cory1:cory2
                pa(i,j)=0;
            end
        end
        
    end
    cory1=cory1+ydir(indy);
end


corx2=1;
corx1=1;
for indx=1:lx+1
    corx2=corx2+xdir(indx);
    
    for indy=1:ly
    
        cory=reservior+(indy-1)*c+down;
        %-------------------------
      tmp=rand;mark=0;
    for (j=2:r_max-r_min+2)
        if (tmp>=dist(j-1)) && (tmp<dist(j)) &&(mark==0)
            test_data=j-1;
            mark=1;
        end
    end
        chw=test_data;deb=[deb,chw];
         %-------------------------
         
         %------------------------
         texts=[texts;[(corx1+corx2)/2,cory+ch_int(chw)+shift,chw]];
         %------------------------
        for i=corx1:corx2
            for j=cory-ch_int(chw):cory+ch_int(chw)
                pa(i,j)=0;
            end
        end
        
    end
    corx1=corx1+xdir(indx);
end
%============================================


%===============side channels================
% side_w=9;
% for i=1:side_w
%     for j=reservior+down:ny
%         pa(i,j)=0;
%     end
% end
% 
% for i=nx-side_w+1:nx
%     for j=reservior+down:ny
%         pa(i,j)=0;
%     end
% end
%============================================

figure
imshow(pa);


figure
imshow(pa);
[tx,ty]=size(texts);
for i=1:tx
    text(texts(i,2),texts(i,1),int2str(texts(i,3)),'FontSize',13);
end

[tx,ty]=size(texts2);
for i=1:tx
    text(texts2(i,2),texts2(i,1),int2str(texts2(i,3)),'FontSize',13,'Color', 'w');
end
% text(160,160,sc,'FontSize',18)
% text(190,160,'sfd','FontSize',18)

%=============STATISTICAL VALIDATION====================
stas=zeros(r_max);



[tx,ty]=size(deb);
for i=1:ty
    stas(deb(1,i))=stas(deb(1,i))+1;
end


figure 
%plot(deb);
plot (r_min:r_max,num);
hold on
plot (ref2(:,1),ref2(:,2),'b');
hold on
plot(r_min:r_max,stas(r_min:r_max,1)/ty,'rx-');
hold off
legend('analytical','random distribution');
%=======================================================

%============================================
% fid = fopen('Emily_JunctionS.dat','wt');
% 
% for k=1:d+2
%     for j=1:ny
%         for i=1:nx
%         if ((k==1) || (k==d+2))
%             if (j<=reservior)
%                 fprintf(fid,'%1d\n',0);
%             else
%                 fprintf(fid,'%1d\n',1);
%             end
%         else
%             fprintf(fid,'%1d\n',pa(i,j));
%         end
%         end
%     end
% end
%=============================================

nx
ny
d+2

